package su.test.bidirectionalEncryption.asymmetricEncryption;

/**
 * @author SuZuQi
 * @title: EncryRSA
 * @projectName myEncry
 * @description: TODO
 * @date 2019/12/17
 */

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.security.*;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;

/**
 * 1.原理：将两个大素数相乘十分容易，但那时想要对其乘积进行因式分解却极其困难，因此可以将乘积公开作为加密密钥
 * 2.开发人员：Ron Rivest、Adi Shamirh和LenAdleman
 */
public class EncryRSA {

    private static Cipher cipher;

    static {
        try {
            cipher = Cipher.getInstance("RSA");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        }
    }

    /********************************公钥加密、私钥解密*******************************************/
    public static byte[] encryByPublicKey(RSAPublicKey publicKey, String text) throws InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        return cipher.doFinal(text.getBytes());
    }

    public static byte[] decryByPrivateKey(RSAPrivateKey privateKey, byte[] encryBy) throws InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        return cipher.doFinal(encryBy);
    }

    /********************************公钥加密、私钥解密*******************************************/

//----------------------------------私钥加密，公钥解密----------------------------------------------------
    public static byte[] encryByPrivateKey(RSAPrivateKey privateKey,String text) throws InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
        cipher.init(Cipher.ENCRYPT_MODE, privateKey);
        return cipher.doFinal(text.getBytes());
    }

    public static byte[] decryByPublicKey(RSAPublicKey publicKey, byte[] encryBy) throws InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
        cipher.init(Cipher.DECRYPT_MODE, publicKey);
        return cipher.doFinal(encryBy);
    }

    //----------------------------------私钥加密，公钥解密----------------------------------------------------


    public static void main(String[] args) throws NoSuchAlgorithmException, BadPaddingException, InvalidKeyException, IllegalBlockSizeException {
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
        keyPairGenerator.initialize(1024);
        KeyPair keyPair = keyPairGenerator.generateKeyPair();
        PrivateKey aPrivate = keyPair.getPrivate();
        PublicKey aPublic = keyPair.getPublic();
        String love = "I love Huang ChunYuan";
        System.out.println(love);
        byte[] encry = encryByPublicKey((RSAPublicKey) aPublic, love);
        System.out.println(new String(encry));
        System.out.println(new String(decryByPrivateKey((RSAPrivateKey) aPrivate, encry)));

        System.out.println("11111111111111111111111111111111111111111111111");
        byte[] encry2 = encryByPrivateKey((RSAPrivateKey) aPrivate, love);
        System.out.println(new String(encry2));
        System.out.println(new String(decryByPublicKey((RSAPublicKey) aPublic, encry2)));
    }
}
